const fs = require('fs');
const javaStringSplitter = require('../java/java-string-splitter');
const path = require('path');
const webpack = require("webpack");

const OUT_JS = path.join(__dirname, '..', 'out', 'bundle.js');
const OUT_JAVA = path.join(__dirname, '..', '..', 'processors', 'src', 'main',
    'java', 'com',
    'axellience', 'vuegwt', 'processors', 'component', 'template', 'builder',
    'compiler', 'JsVueTemplateCompiler.java');

module.exports = function () {
  webpack({
    entry: './js-vue-template-compiler.js',
    output: {
      path: path.join(__dirname, '..', 'out'),
      filename: 'bundle.js'
    },
    optimization: {
      minimize: true
    },
    target: 'node'
  }, (err) => {
    if (err) {
      console.error(err);
    }

    fs.readFile(OUT_JS, 'utf8', function (err, data) {
      if (err) {
        throw err;
      }

      const javaStringBuilder = javaStringSplitter.splitStringForJava(data,
          'JS_VUE_TEMPLATE_COMPILER');
      fs.writeFile(OUT_JAVA, `package com.axellience.vuegwt.processors.component.template.builder.compiler;

/**
This class is generated by utils/js-vue-template-compiler/process-js-vue-template-compiler.js
*/
class JsVueTemplateCompiler
{
    public static String JS_VUE_TEMPLATE_COMPILER;

    static
    {
    	${javaStringBuilder}
    }
}`, () => console.log('Process Js Vue Template Compiler SUCCESS'));
    });
  });
};
